#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

# Autoconf basics
AC_PREREQ(2.61)
AC_INIT([Netdude], [0.5.1], [netdude-devel@lists.sourceforge.net])
AC_CONFIG_SRCDIR([src/nd.h])
AC_CONFIG_HEADER([config.h])
# AC_CONFIG_AUX_DIR([scripts])
# AC_CONFIG_MACRO_DIR([m4])

# Initialize automake
AM_INIT_AUTOMAKE


dnl ##################################################
dnl # Set version info in config.h
dnl ##################################################
version_major=0.5
version_minor=1
AC_SUBST(version_major)
AC_SUBST(version_minor)
AC_DEFINE_UNQUOTED(VERSION_MAJOR, "$version_major", [Major package version])
AC_DEFINE_UNQUOTED(VERSION_MINOR, "$version_minor", [Minor package version])


dnl Commands for funkier shtool output:
BLD_ON=`./shtool echo -n -e %B`
BLD_OFF=`./shtool echo -n -e %b`

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

# Checks for libraries
PKG_CHECK_MODULES([GTK], [gtk+-2.0 >= 2.4.0])

# Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS([arpa/inet.h libintl.h netdb.h netinet/in.h stdlib.h string.h sys/param.h sys/socket.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_OFF_T

# Checks for library functions.
AC_FUNC_CLOSEDIR_VOID
AC_FUNC_REALLOC
AC_TYPE_SIGNAL
AC_FUNC_STAT
AC_CHECK_FUNCS([atexit getcwd inet_ntoa memmove memset strdup strerror strrchr strstr strtol strtoul])

# Libtool/libltdl support
AC_LIBLTDL_CONVENIENCE
AC_CONFIG_SUBDIRS([libltdl])
AC_LIBTOOL_DLOPEN
AM_PROG_LIBTOOL
AC_DEFINE(LT_NON_POSIX_NAMESPACE, 1, [For libtool -- what exactly was this for again???])

# Check host endianness
AC_C_BIGENDIAN

# Arrange for large file support
AC_SYS_LARGEFILE


dnl ##################################################
dnl # Check canonical host system type
dnl ##################################################

AM_CONDITIONAL(SOLARIS_HOST, false)
AM_CONDITIONAL(LINUX_HOST, false)
AM_CONDITIONAL(BSD_HOST, false)
AM_CONDITIONAL(APPLE_HOST, false)

AC_CANONICAL_HOST

case "$host" in
  *-solaris*)
    AC_DEFINE(SOLARIS_HOST, 1, [Whether this is a Solaris host])
    AM_CONDITIONAL(SOLARIS_HOST, true)
    ;;
  *-linux*)
    AC_DEFINE(LINUX_HOST, 1, [Whether this is a Linux host])
    AM_CONDITIONAL(LINUX_HOST, true)
    ;;
  *bsd*)
    AC_DEFINE(BSD_HOST, 1, [Whether this is a BSD host])
    AM_CONDITIONAL(BSD_HOST, true)
    ;;
  *-apple*)
    AC_DEFINE(BSD_HOST, 1, [Whether this is a BSD host])
    AM_CONDITIONAL(BSD_HOST, true)
    AC_DEFINE(APPLE_HOST, 1, [Whether this is an Apple host])
    AM_CONDITIONAL(APPLE_HOST, true)
    ;;
esac


dnl ##################################################
dnl # Set various package-level variables in config.h
dnl ##################################################

# By temporarily setting the prefix variable to that of
# ac_default_prefix if prefix is unchanged from the default NONE, we
# can easily obtain the values for a number of relevant
# directories. Store original value of prefix variable, so we can
# revert later.
#
prefix_orig="${prefix}"
exec_prefix_orig="${exec_prefix}"

if test "x${prefix}" = "xNONE" -o "x${prefix}" = "x"; then
   prefix="${ac_default_prefix}"
fi 
if test "x${exec_prefix}" = "xNONE" -o "x${exec_prefix}" = "x"; then
   exec_prefix="${prefix}"
fi 

packageprefix=`eval eval eval eval "echo $prefix"`
AC_DEFINE_UNQUOTED(PACKAGE_PREFIX, "${packageprefix}", [The installation prefix of the package])

packagelocaledir=`eval eval eval eval "echo ${prefix}/locale"`
AC_DEFINE_UNQUOTED(PACKAGE_LOCALE_DIR, "${packagelocaledir}", [The locale dir of the package])

PACKAGE_DATA_DIR=`eval eval eval eval "echo $datadir"`
AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${PACKAGE_DATA_DIR}", [The data dir of the package])
AC_SUBST(PACKAGE_DATA_DIR)

PACKAGE_SOURCE_DIR=`cd $srcdir && pwd`
AC_DEFINE_UNQUOTED(PACKAGE_SOURCE_DIR, "${PACKAGE_SOURCE_DIR}", [The source dir of the package])
AC_SUBST(PACKAGE_SOURCE_DIR)

PACKAGE_INCLUDE_DIR=`eval eval eval eval "echo $includedir"`
AC_DEFINE_UNQUOTED(PACKAGE_INCLUDE_DIR, "${PACKAGE_INCLUDE_DIR}", [The header dir being used])
AC_SUBST(PACKAGE_INCLUDE_DIR)

# Revert prefix to original value.
prefix="${prefix_orig}"
exec_prefix="${exec_prefix_orig}"


dnl ##################################################
dnl # Check for libnetdude
dnl ##################################################
AC_ARG_WITH(lndtool,
    AC_HELP_STRING([--with-lndtool=FILE], [Use given lndtool]),
    [ lndtool="$withval" ],
    [ AC_PATH_PROG(lndtool, lndtool,
                   AC_MSG_ERROR(Cannot find lndtool: is it in path?)) ])

LIBNETDUDE_CFLAGS=`$lndtool --cflags`
LIBNETDUDE_DIR=`$lndtool --plugin-dir`
LIBNETDUDE_LIBS=`$lndtool --libs`
AC_SUBST(LIBNETDUDE_CFLAGS)
AC_SUBST(LIBNETDUDE_DIR)
AC_SUBST(LIBNETDUDE_LIBS)


dnl ##################################################
dnl # Check for FAM, the file alteration monitor.
dnl ##################################################
AC_ARG_WITH(fam,
            AC_HELP_STRING([--with-fam=DIR], [Use fam in <DIR>]),
            [CPPFLAGS="$CPPFLAGS -I$withval/include"; LDFLAGS="$LDFLAGS -L$withval/lib"])
AC_CHECK_HEADERS([fam.h], , fam_header_ok="no")
AC_CHECK_LIB(fam, main, , fam_lib_ok="no")
if test "$fam_header_ok" = "no" -o "$fam_lib_ok" = "no"; then
    fam="no"
else
    fam="yes"
fi;

##################################################
# Check for gtk-doc.
##################################################

AC_ARG_WITH(html-dir, [  --with-html-dir=PATH path to installed docs ])

if test "x$with_html_dir" = "x" ; then
  HTML_DIR='${datadir}/gtk-doc/html'
else
  HTML_DIR=$with_html_dir
fi

AC_SUBST(HTML_DIR)

AC_CHECK_PROG(GTKDOC, gtkdoc-mkdb, true, false)

gtk_doc_min_version_maj=0
gtk_doc_min_version_min=6

if $GTKDOC ; then 
    gtk_doc_version=`gtkdoc-mkdb --version`
    AC_MSG_CHECKING([gtk-doc version ($gtk_doc_version) >= $gtk_doc_min_version_maj.$gtk_doc_min_version_min])
    if perl <<EOF ; then
      exit (("$gtk_doc_version" =~ /^(\d+)\.(\d+)$/ &&
            (("\$1" > "$gtk_doc_min_version_maj") ||
	     (("\$1" == "$gtk_doc_min_version_maj") &&
	      ("\$2" >= "$gtk_doc_min_version_min")))) ? 0 : 1);
EOF
      AC_MSG_RESULT(yes)
   else
      AC_MSG_RESULT(no)
      GTKDOC=false
   fi
fi

dnl Let people disable the gtk-doc stuff.
AC_ARG_ENABLE(gtk-doc, [  --enable-gtk-doc  Use gtk-doc to build documentation [default=auto]], enable_gtk_doc="$enableval", enable_gtk_doc=auto)

if test x$enable_gtk_doc = xauto ; then
  if test x$GTKDOC = xtrue ; then
    enable_gtk_doc=yes
  else
    enable_gtk_doc=no 
  fi
fi

AM_CONDITIONAL(ENABLE_GTK_DOC, test x$enable_gtk_doc = xyes)


dnl ##################################################
dnl # Debugging enable/disable switch
dnl ##################################################
AC_ARG_ENABLE(debugging,
	      AC_HELP_STRING([--enable-debugging], [Use debugging macros to produce helpful output (disabled by default)]),
	      debugging="yes",
	      debugging="no")

if test x$debugging = xyes; then
  AC_DEFINE_UNQUOTED(ND_DEBUG, 1, [Enable debugging output])
fi

dnl AC_OUTPUT_COMMANDS([if test ! -e intl/libintl.h; then ln -s libgnuintl.h intl/libintl.h; fi])

dnl Make sure our version of the mkhtml utility is executable:
AC_CONFIG_COMMANDS([mkhtml], [chmod 755 $ac_top_srcdir/doc/netdude-manual/mkhtml])


dnl ##################################################
dnl # Release build enable/disable switch
dnl ##################################################
AC_DEFINE_UNQUOTED(RELEASE, 1, [Enable release build])


dnl ##################################################
dnl # This is the GTK+ 2.x compatible Makefile list
dnl ##################################################
AC_CONFIG_FILES([Makefile
                 doc/Makefile
                 doc/netdude-manual/Makefile
                 m4/Makefile
                 pixmaps/Makefile
                 src/Makefile
				 protocols/Makefile
				 protocols/dummy/Makefile
				 protocols/ether/Makefile
				 protocols/ip/Makefile
				 protocols/tcp/Makefile])

dnl ##################################################
dnl # This is the GTK+ 1.x compatible Makefile list
dnl ##################################################
dnl AC_CONFIG_FILES([plugins/BPF-Filter/Makefile
dnl                  plugins/ChecksumFix/Makefile
dnl                  plugins/Makefile
dnl                  protocols/Makefile
dnl                  protocols/arp/Makefile
dnl                  protocols/ether/Makefile
dnl                  protocols/fddi/Makefile
dnl                  protocols/icmp/Makefile
dnl                  protocols/ip/Makefile
dnl                  protocols/linux-sll/Makefile
dnl                  protocols/pcap/Makefile
dnl                  protocols/snap/Makefile
dnl                  protocols/tcp/Makefile
dnl                  protocols/udp/Makefile
dnl                  protocols/vlan/Makefile])

AC_OUTPUT

dnl ##################################################
dnl # Print summary
dnl ##################################################

echo
echo "         "${BLD_ON}"Netdude $version_major Configuration Summary"${BLD_OFF}
echo "===================================================="
echo

echo "  libnetdude configuration: "${BLD_ON}"$lndtool"${BLD_OFF}
echo "  FAM support:              "${BLD_ON}"$fam"${BLD_OFF}
echo "  Debugging enabled:        "${BLD_ON}"$debugging"${BLD_OFF}
echo
echo "  Setup finished. Now run:"
echo
echo "  $ "${BLD_ON}"make"${BLD_OFF}
echo "  # "${BLD_ON}"make install"${BLD_OFF}
echo
echo "  (or use "${BLD_ON}"gmake"${BLD_OFF}" when make on your platform isn't GNU make)"
echo
